package com.ruoyi.qsng.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.SnowFlakeUtil;
import com.ruoyi.qsng.domain.KUsers;
import com.ruoyi.qsng.domain.KUsersInformation;
import com.ruoyi.qsng.mapper.KUsersInformationMapper;
import com.ruoyi.qsng.mapper.KUsersMapper;
import com.ruoyi.qsng.service.IKUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Slf4j
@Service
public class KUsersServiceImpl implements IKUsersService {
    public static final String APP_ID = "wx2c8c2c1fc2b4d594";
    public static final String APP_SECRET = "8662e3ef59e0384cd6edc4d34d32353a";


    @Autowired
    private KUsersMapper kUsersMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private KUsersInformationMapper kUsersInformationMapper;

    @Override
    public List<KUsers> selectKUsersOrderByIntegral() {
        List<KUsers> kUsersList = kUsersMapper.selectKUsersOrderByIntegral();

        return kUsersList;
    }

    /**
     * 查询用户信息
     *
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public KUsers selectKUsersById(String id) {
        return kUsersMapper.selectKUsersById(id);
    }

    /**
     * 查询用户信息列表
     *
     * @param kUsers 用户信息
     * @return 用户信息
     */
    @Override
    public List<KUsers> selectKUsersList(KUsers kUsers) {
        return kUsersMapper.selectKUsersList(kUsers);
    }

    /**
     * 新增用户信息
     *
     * @param kUsers 用户信息
     * @return 结果
     */
    @Override
    public int insertKUsers(KUsers kUsers) {
        kUsers.setId(SnowFlakeUtil.getNextId());
        kUsers.setCreateTime(DateUtils.getNowDate());
        return kUsersMapper.insertKUsers(kUsers);
    }

    /**
     * 修改用户信息
     *
     * @param kUsers 用户信息
     * @return 结果
     */
    @Override
    public int updateKUsers(KUsers kUsers) {
        KUsers kUsers1 = kUsersMapper.selectKUsersById(kUsers.getId());
        kUsers.setUpdateTime(DateUtils.getNowDate());
        KUsersInformation kUsersInformation = new KUsersInformation();
        kUsersInformation.setUserName(kUsers.getName());
        kUsersInformation.setAvatar(kUsers.getAvatarUrl());
//        kUsersInformation.setPhone(kUsers1.getUserName());
        kUsersInformation.setNickName(kUsers.getNickName());
        kUsersInformation.setNickAge(kUsers.getNickAge());
        kUsersInformation.setNickNation(kUsers.getNickNation());
        kUsersInformation.setNickCard(kUsers.getNickCard());
        kUsersInformation.setHomeName(kUsers.getHomeName());
        kUsersInformation.setHomePhone(kUsers.getHomePhone());
        kUsersInformation.setNickSchool(kUsers.getNickSchool());
        kUsersInformation.setNickLevel(kUsers.getNickLevel());
        kUsersInformation.setOpenid(kUsers1.getOpenId());
        kUsersInformationMapper.updateKUsersInformationByPhone(kUsersInformation);
        return kUsersMapper.updateKUsers(kUsers);
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteKUsersByIds(String[] ids) {
        return kUsersMapper.deleteKUsersByIds(ids);
    }

    /**
     * 删除用户信息信息
     *
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteKUsersById(String id) {
        return kUsersMapper.deleteKUsersById(id);
    }
}
